/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.dp.services;

import com.dp.model.Articles;
import com.dp.model.ArticlesDao;
import com.dp.model.Books;
import com.dp.model.BooksDao;
import com.dp.utils.BaseException;
import java.util.Date;
import java.util.List;

/**
 *
 * @author hisik
 */
public class ItemServices implements IItemServices {
    
    private static BooksDao booksDao;
    private static ArticlesDao articlesDao;

    public ItemServices() {
         booksDao = BooksDao.getInstance();
         articlesDao = ArticlesDao.getInstance();
    }

    public void setBooksDao(BooksDao booksDao) {
        ItemServices.booksDao = booksDao;
    }

    public static void setArticlesDao(ArticlesDao articlesDao) {
        ItemServices.articlesDao = articlesDao;
    }
    
    @Override
    public List<Books> getBooksByName(String name) {
        return booksDao.getBooksByName(name);
    }

    @Override
    public List<Articles> getArticlesByName(String name) {
        return articlesDao.getArticlesByName(name);
    }

    @Override
    public Books saveOrUpdateBooks(Long objid, int authorCount, String authorName, 
            String bookName, String isbn, String issueArea, Date issueDate, 
            int pageCount, String publisherName) throws BaseException {
        
        try {
            Books book = null;
            
            if(objid == null) { // yeni kitap eklenecek
                book = new Books();
            }
            else { // var olan kayıt güncellenecek
                book = booksDao.findByObjId(objid);
                if(book == null)
                    throw new BaseException("Please select an item!");
            }
            
            book.setAuthorCount(authorCount);
            book.setAuthorName(authorName);
            book.setBookName(bookName);
            book.setIsbn(isbn);
            book.setIssueArea(issueArea);
            book.setIssueDate(issueDate);
            book.setPageCount(pageCount);
            book.setPublisherName(publisherName);

            booksDao.saveOrUpdate(book);
                
            return book;
        } catch (BaseException ex) {
            throw new BaseException("Error!");
        }

    }

    @Override
    public Articles saveOrUpdateArticles(Long objid, int authorCount, 
            String articleName, String issue, String issueArea, Date issueDate, 
            String journal, int pageCount, String publisherName, String volume) 
            throws BaseException {
        
        try {
            Articles article = null;
            
            if(objid == null) { // yeni kitap eklenecek
                article = new Articles();
            }
            else { // var olan kayıt güncellenecek
                article = articlesDao.findByObjId(objid);
                if(article == null)
                    throw new BaseException("Please select an item!");
            }
            
            article.setAuthorCount(authorCount);
            article.setArticleName(articleName);
            article.setJournalName(journal);
            article.setVolume(volume);
            article.setIssueArea(issueArea);
            article.setIssueDate(issueDate);
            article.setIssue(issue);
            article.setPageCount(pageCount);
            article.setPublisherName(publisherName);

            articlesDao.saveOrUpdate(article);
                
            return article;
        } catch (BaseException ex) {
            throw new BaseException("Error!");
        }

    }
    
    @Override
    public List<Books> loadBookList() throws BaseException {
        return booksDao.findAll();
    }
    
    @Override
    public List<Articles> loadArticleList() throws BaseException {
        return articlesDao.findAll();
    }

    @Override
    public Books getBooksByObjid(Long objid) throws BaseException {
        return booksDao.findByObjId(objid);
    }

    @Override
    public Articles getArticlesByObjid(Long objid) throws BaseException {
        return articlesDao.findByObjId(objid);
    }
    
    @Override
    public void deleteBookByObjid(Long objid) throws BaseException {
        booksDao.deleteByObjid(objid);
    }

    @Override
    public void deleteArticleByObjid(Long objid) throws BaseException {
        articlesDao.deleteByObjid(objid);
    }

    
}
